package cn.shopthrive.merchants;

import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Build;
import android.util.Log;

import androidx.core.app.NotificationCompat;

import com.huawei.hms.push.HmsMessageService;
import com.huawei.hms.push.RemoteMessage;

import io.flutter.plugin.common.MethodChannel;

public class HmsPushService extends HmsMessageService {
    private static final String TAG = "HmsPushService";
    private static final String CHANNEL = "com.shopthrive.merchants/push";
    private MethodChannel channel;

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        Log.i(TAG, "Received message on channel: " + CHANNEL); // 使用CHANNEL变量
        showNotification(remoteMessage.getNotification().getTitle(), remoteMessage.getNotification().getBody(), remoteMessage.getData());
        if (channel != null) {
            channel.invokeMethod("onMessage", remoteMessage.getNotification().getTitle() + ": " + remoteMessage.getNotification().getBody());
        } else {
            Log.w(TAG, "MethodChannel is null, cannot send message to Flutter on channel: " + CHANNEL);
        }
    }

    @Override
    public void onNewToken(String token) {
        super.onNewToken(token);
        Log.i(TAG, "Received new token: " + token);
        if (channel != null) {
            channel.invokeMethod("onToken", token);
        } else {
            Log.w(TAG, "MethodChannel is null, cannot send token to Flutter");
        }
    }

    public void setChannel(MethodChannel channel) {
        this.channel = channel;
        Log.i(TAG, "MethodChannel set for channel: " + CHANNEL);
    }

    private void showNotification(String title, String body, String data) {
        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        String channelId = "push_channel";

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(channelId, "Push Notifications", NotificationManager.IMPORTANCE_HIGH);
            channel.setLockscreenVisibility(NotificationCompat.VISIBILITY_PUBLIC);
            manager.createNotificationChannel(channel);
        }

        Intent intent = new Intent(this, MainActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.putExtra("notification_data", data != null ? data : "");
        PendingIntent pendingIntent = PendingIntent.getActivity(
                this,
                (int) System.currentTimeMillis(),
                intent,
                Build.VERSION.SDK_INT >= Build.VERSION_CODES.S ? PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE : PendingIntent.FLAG_UPDATE_CURRENT
        );

        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, channelId)
                .setSmallIcon(android.R.drawable.ic_dialog_info)
                .setContentTitle(title)
                .setContentText(body)
                .setAutoCancel(true)
                .setPriority(NotificationCompat.PRIORITY_HIGH)
                .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
                .setContentIntent(pendingIntent);

        manager.notify((int) System.currentTimeMillis(), builder.build());
    }
}